package com.gitee.wsl.time

import kotlin.time.Duration


/**
 * A standard set of date periods units.
 *
 *
 * This set of units provide unit-based access to manipulate a date, time or date-time.
 * The standard set of units can be extended by implementing [TemporalUnit].
 *
 *
 * These units are intended to be applicable in multiple calendar systems.
 * For example, most non-ISO calendar systems define units of years, months and days,
 * just with slightly different rules.
 * The documentation of each unit explains how it operates.
 *
 * @implSpec
 * This is a final, immutable and thread-safe enum.
 *
 * @since 1.8
 */
enum class ChronoUnit(
    val nameString: String,
    val duration: Duration)  {
    /**
     * Unit that represents the concept of a nanosecond, the smallest supported unit of time.
     * For the ISO calendar system, it is equal to the 1,000,000,000th part of the second unit.
     */
    NANOS("Nanos", Duration.ofNanos(1)),

    /**
     * Unit that represents the concept of a microsecond.
     * For the ISO calendar system, it is equal to the 1,000,000th part of the second unit.
     */
    MICROS("Micros", Duration.ofNanos(1000)),

    /**
     * Unit that represents the concept of a millisecond.
     * For the ISO calendar system, it is equal to the 1000th part of the second unit.
     */
    MILLIS("Millis", Duration.ofNanos(1000000)),

    /**
     * Unit that represents the concept of a second.
     * For the ISO calendar system, it is equal to the second in the SI system
     * of units, except around a leap-second.
     */
    SECONDS("Seconds", Duration.ofSeconds(1)),

    /**
     * Unit that represents the concept of a minute.
     * For the ISO calendar system, it is equal to 60 seconds.
     */
    MINUTES("Minutes", Duration.ofSeconds(60)),

    /**
     * Unit that represents the concept of an hour.
     * For the ISO calendar system, it is equal to 60 minutes.
     */
    HOURS("Hours", Duration.ofSeconds(3600)),

    /**
     * Unit that represents the concept of half a day, as used in AM/PM.
     * For the ISO calendar system, it is equal to 12 hours.
     */
    HALF_DAYS("HalfDays", Duration.ofSeconds(43200)),

    /**
     * Unit that represents the concept of a day.
     * For the ISO calendar system, it is the standard day from midnight to midnight.
     * The estimated duration of a day is `24 Hours`.
     *
     *
     * When used with other calendar systems it must correspond to the day defined by
     * the rising and setting of the Sun on Earth. It is not required that days begin
     * at midnight - when converting between calendar systems, the date should be
     * equivalent at midday.
     */
    DAYS("Days", Duration.ofSeconds(86400)),

    /**
     * Unit that represents the concept of a week.
     * For the ISO calendar system, it is equal to 7 days.
     *
     *
     * When used with other calendar systems it must correspond to an integral number of days.
     */
    WEEKS("Weeks", Duration.ofSeconds(7 * 86400L)),

    /**
     * Unit that represents the concept of a month.
     * For the ISO calendar system, the length of the month varies by month-of-year.
     * The estimated duration of a month is one twelfth of `365.2425 Days`.
     *
     *
     * When used with other calendar systems it must correspond to an integral number of days.
     */
    MONTHS("Months", Duration.ofSeconds(31556952L / 12)),

    /**
     * Unit that represents the concept of a year.
     * For the ISO calendar system, it is equal to 12 months.
     * The estimated duration of a year is `365.2425 Days`.
     *
     *
     * When used with other calendar systems it must correspond to an integral number of days
     * or months roughly equal to a year defined by the passage of the Earth around the Sun.
     */
    YEARS("Years", Duration.ofSeconds(31556952L)),

    /**
     * Unit that represents the concept of a decade.
     * For the ISO calendar system, it is equal to 10 years.
     *
     *
     * When used with other calendar systems it must correspond to an integral number of days
     * and is normally an integral number of years.
     */
    DECADES("Decades", Duration.ofSeconds(31556952L * 10L)),

    /**
     * Unit that represents the concept of a century.
     * For the ISO calendar system, it is equal to 100 years.
     *
     *
     * When used with other calendar systems it must correspond to an integral number of days
     * and is normally an integral number of years.
     */
    CENTURIES("Centuries", Duration.ofSeconds(31556952L * 100L)),

    /**
     * Unit that represents the concept of a millennium.
     * For the ISO calendar system, it is equal to 1000 years.
     *
     *
     * When used with other calendar systems it must correspond to an integral number of days
     * and is normally an integral number of years.
     */
    MILLENNIA("Millennia", Duration.ofSeconds(31556952L * 1000L)),

    /**
     * Unit that represents the concept of an era.
     * The ISO calendar system doesn't have eras thus it is impossible to add
     * an era to a date or date-time.
     * The estimated duration of the era is artificially defined as `1,000,000,000 Years`.
     *
     *
     * When used with other calendar systems there are no restrictions on the unit.
     */
    ERAS("Eras", Duration.ofSeconds(31556952L * 1000000000L)),

    /**
     * Artificial unit that represents the concept of forever.
     * This is primarily used with [TemporalField] to represent unbounded fields
     * such as the year or era.
     * The estimated duration of this unit is artificially defined as the largest duration
     * supported by [Duration].
     */
    FOREVER("Forever", Duration.ofSeconds(Long.MAX_VALUE, 999999999));


    //-----------------------------------------------------------------------
    /**
     * Gets the estimated duration of this unit in the ISO calendar system.
     *
     *
     * All of the units in this class have an estimated duration.
     * Days vary due to daylight saving time, while months have different lengths.
     *
     * @return the estimated duration of this unit, not null
     */


    val isDurationEstimated: Boolean
        /**
         * Checks if the duration of the unit is an estimate.
         *
         *
         * All time units in this class are considered to be accurate, while all date
         * units in this class are considered to be estimated.
         *
         *
         * This definition ignores leap seconds, but considers that Days vary due to
         * daylight saving time and months have different lengths.
         *
         * @return true if the duration is estimated, false if accurate
         */
        get() = this >= DAYS

    //-----------------------------------------------------------------------
    val isDateBased: Boolean
        /**
         * Checks if this unit is a date unit.
         *
         *
         * All units from days to eras inclusive are date-based.
         * Time-based units and `FOREVER` return false.
         *
         * @return true if a date unit, false if a time unit
         */
        get() = this >= DAYS && this != FOREVER

    val isTimeBased: Boolean
        /**
         * Checks if this unit is a time unit.
         *
         *
         * All units from nanos to half-days inclusive are time-based.
         * Date-based units and `FOREVER` return false.
         *
         * @return true if a time unit, false if a date unit
         */
        get() = this.compareTo(ChronoUnit.DAYS) < 0

    //-----------------------------------------------------------------------
//     fun isSupportedBy(temporal: Temporal): Boolean {
//        return temporal.isSupported(this)
//    }
//
//     fun <R : Temporal?> addTo(temporal: R?, amount: Long): R? {
//        return temporal.plus(amount, this) as R?
//    }
//
//    //-----------------------------------------------------------------------
//     fun between(
//        temporal1Inclusive: Temporal,
//        temporal2Exclusive: Temporal?,
//    ): Long {
//        return temporal1Inclusive.until(temporal2Exclusive, this)
//    }

    //-----------------------------------------------------------------------
    override fun toString(): String {
        return name
    }
}